import { SearchResultResponse, SuggestionResponse } from "@/types/data";
import { RootThunkAction } from "@/types/store";
import { http } from "@/utils/http";
// 渲染联想关键词
export const getSuggestion = (value: string): RootThunkAction => {
  return async (dispatch) => {
    const res = await http.get<SuggestionResponse>("/suggestion", {
      params: {
        q: value,
      },
    });

    dispatch({ type: "search/suggestion", payload: res.data.data.options });
  };
};
// 清空联想关键词
export const clearSuggestion = () => ({ type: "search/clearSuggestion" });

export const getSearchResult = (query: string, page: any): RootThunkAction => {
  return async (dispatch) => {
    const res = await http.get<SearchResultResponse>("/search", {
      params: {
        q: query,
        page: page,
      },
    });

    dispatch({ type: "search/getSearchResult", payload: res.data.data });
  };
};
export const clearSearchResult = () => {
  return async (dispatch: any) => {
    dispatch({ type: "search/clearSearchResult", payload: "" });
  };
};
